//
//  GHMineViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHMineViewController.h"
#import "GHLoginViewController.h"
#import "GHMineHeaderView.h"
#import "GHTableEasyModel.h"
#import "GHMIneTableViewCell.h"
#import "GHSettingViewController.h"
#import "GHChongZhiRecordController.h"
#import "GHTixianRecordViewController.h"
#import "GHAboutUsViewController.h"
#import "GHMineOrderViewController.h"
#import "GHShopCardViewController.h"
#import "GHRecommendViewController.h"
#import "GHWithdrawViewController.h"
#import "GHRechargeViewController.h"
#import "GHConsumeViewController.h"
#import "GHMerchantInputController.h"
#import "GHGiveViewController.h"
#import "STWebViewController.h"
#import "GHFromViewController.h"
#import "GHMerchnFormViewController.h"

@interface GHMineViewController ()<GHMineHeaderViewDelegate>
@property(nonatomic, strong) GHMineHeaderView                                *headerView;
@property(nonatomic, strong) GHUserModel                                     *userModel;
@property(nonatomic, strong) NSArray <NSArray*>                              *dataSouce;
@property(nonatomic, strong) NSString                     *confirm_bill;//待确定的订单数
@end

@implementation GHMineViewController
- (void)setUserModel:(GHUserModel *)userModel{
    _userModel = userModel;
    _headerView.userModel = userModel;
    //_userModel.user_type = @"3";
    [self.tableView reloadData];
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.frame = CGRectMake(0, 20, UIScreenWidth, UIScreenHeight - 64);
    self.headerView = [[GHMineHeaderView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100 )];
    self.headerView.delegate = self;
    self.tableView.tableHeaderView = self.headerView;
    

}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self st_hideNavagetionbarAnimated:animated];
    [self sendUserInfoRequest];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
   
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self st_showNavagationbarAnimated:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)reloadData{
    self.confirm_bill = self.confirm_bill.integerValue? self.confirm_bill:@"";
    NSString  * confirm_bill = [NSString stringWithFormat:@"确认报单%@",self.confirm_bill];
    if ([self.userModel.user_type isEqualToString:@"3"]) {
        confirm_bill = [NSString stringWithFormat:@"报单中心%@",self.confirm_bill];
    }
    NSArray * sectionOne = @[
                             [[GHTableEasyModel alloc] initWithTextString:@"推荐明细" imageName:@"PC_tuijianmingxi"],
                             [[GHTableEasyModel alloc] initWithTextString:@"充值记录" imageName:@"PC_tixian"],
                             [[GHTableEasyModel alloc] initWithTextString:@"提现记录" imageName:@"PC_tixianjilu"],
                             [[GHTableEasyModel alloc] initWithTextString:@"消费记录" imageName:@"PC_xiaofeijilu"]];
    NSArray * sectionTwo= @[
                            [[GHTableEasyModel alloc] initWithTextString:@"商家入驻" imageName:@"PC_shangjiaruzhu"],
                            [[GHTableEasyModel alloc] initWithTextString:confirm_bill imageName:@"PC_tuijianmingxi"],
                            [[GHTableEasyModel alloc] initWithTextString:@"购物车" imageName:@"PC_daifahuo"],
                            [[GHTableEasyModel alloc] initWithTextString:@"赠送明细" imageName:@"PC_zhanghumingxi"],
                            [[GHTableEasyModel alloc] initWithTextString:@"邀请好友" imageName:@"PC_yaoqinghaoyou"]];
    
    NSMutableArray * dealArray = [NSMutableArray arrayWithArray:sectionTwo];
    if ([self.userModel.user_type isEqualToString:@"3"]) {
        //商家，没有商家入驻
        [dealArray removeObjectAtIndex:0];
    }else{
        //普通 报单
        [dealArray removeObjectAtIndex:1];
    }
    
    self.dataSouce = @[sectionOne,dealArray];
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.dataSouce[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 45;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    GHMIneTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[GHMIneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    GHTableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.ghImageView.image = model.image;
    cell.lable.text = model.textString;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GHTableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    if (indexPath.row == 0 && indexPath.section == 0) {
        //推荐明细
        [self.navigationController pushViewController:[GHRecommendViewController new] animated:YES];
    }
    if (indexPath.row == 1 && indexPath.section == 0) {
        //充值记录
        [self.navigationController pushViewController:[GHChongZhiRecordController new] animated:YES];
    }
    if (indexPath.row == 2 && indexPath.section == 0) {
        //提现记录
        [self.navigationController pushViewController:[GHTixianRecordViewController new] animated:YES];
    }
    if (indexPath.row == 3 && indexPath.section == 0) {
        //消费记录
        [self.navigationController pushViewController:[GHConsumeViewController new] animated:YES];
    }
    

    if ([model.textString isEqualToString:@"商家入驻"]) {
        [self.navigationController pushViewController:[GHMerchantInputController new] animated:YES];
    }
    if ([model.textString containsString:@"报单中心"]) {
         [self.navigationController pushViewController:[GHFromViewController new] animated:YES];
    }
    if ([model.textString isEqualToString:@"购物车"]) {
        [self.navigationController pushViewController:[GHShopCardViewController new] animated:YES];
    }
    if ([model.textString isEqualToString:@"赠送明细"]) {
        GHGiveViewController * vc = [GHGiveViewController new];
        vc.userModel = self.userModel;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if ([model.textString isEqualToString:@"邀请好友"]) {
        NSString * url = @"http://www.smqyp.com/Home/system/friends";
        url = [NSString stringWithFormat:@"%@?uid=%@",url,[GHUserManger defult].userID];
        STWebViewController * webVC = [[STWebViewController alloc] initWithUrl:url];
        webVC.progressBackgroundColor = GH_backgroundColor;
        webVC.progressTintColor = GH_YellowBackGroundColor;
        webVC.forceTitle = @"邀请好友";
        [self.navigationController pushViewController:webVC animated:YES];
    }


    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    // [self onSelectedLoginButton];
}
#pragma mark --Action Method
- (void)onSelectedLoginButton{
    GHNavgationViewController * nav = [[GHNavgationViewController alloc] initWithRootViewController:[GHLoginViewController new]];
    [self presentViewController:nav animated:YES completion:nil];
}
#pragma mark --GHMineHeaderViewDelegate
- (void)onSelectedIconButton{
    
}
- (void)onSelectedSettingButton{
    GHSettingViewController * vc = [GHSettingViewController new];
    vc.userModel = self.userModel;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)onSelectedChongzhiButton{
    [self.navigationController pushViewController:[GHRechargeViewController new] animated:YES];
}
- (void)onSelectedTiXianButton{
    [self.navigationController pushViewController:[GHWithdrawViewController new] animated:YES];
}
- (void)onSlectedOrderButton:(NSString*)title{
    if ([title isEqualToString:@"待付款"]) {

    }
    if ([title isEqualToString:@"待发货"]) {

    }
    if ([title isEqualToString:@"待收货"]) {

    }
    if ([title isEqualToString:@"待评价"]) {
        
    }
    if ([title isEqualToString:@"已完成"]) {

    }
    GHMineOrderViewController *vc = [GHMineOrderViewController new];
    vc.currentTitle = title;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --NetWork Method
- (void)sendUserInfoRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/personal"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 NSDictionary * data = [responseObject valueForKey:@"data"];
                                                 if ([data.allKeys containsObject:@"confirm_bill"]) {
                                                     self.confirm_bill = data[@"confirm_bill"];
                                                 }
                                                 if ([[data allKeys] containsObject:@"user_info"]) {
                                                     NSDictionary * user_info = [data valueForKey:@"user_info"];
                                                     GHUserModel * user = [GHUserModel mj_objectWithKeyValues:user_info];
                                                     self.userModel = user;
                                                 }
                                                 [self reloadData];

                                             } failure:^(NSString *stateCode, STError *error) {
                                                 NSLog(@"error:%@",error.desc);
                                             }];
}
@end
